import re
import urllib3
import os
from lxml import etree

class S_wm(object):
    def __init__(self):
        self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3872.400 QQBrowser/10.8.4455.400'}
        self.http = urllib3.PoolManager()
    def get_url_list(self,urls):
        res = self.http.request('get', urls, headers=self.headers)
        # print(f'res:>>>{res}{type(res)}')
        html = res.data.decode('gbk')
        # print(f'html:>>>{html}')
        # url_list = re.findall('<img src="(.*?)">', html)
        html1 = etree.HTML(html)
        # print(type(html1))
        url_list = html1.xpath('//li/a/img/@src')
        # 用正则表达式
        # import re
        # url_list = re.findall('<img src="(.*?)" alt=".*?" />', html1, re.S)
        # print(f'url_list:>>>{type(url_list)}')
        return url_list
    def save_image(self,filename,img):
        with open(filename, 'wb') as f:
            f.write(img.data)
        print('图片提取成功')
    def run(self,url):
        url_list = self.get_url_list(url)
        print(url_list)
        for u in url_list:
            file_name = './wm/{}'.format(u.split('/')[-1])
            data = self.http.request('get', f'https://pic.netbian.com{u}', headers=self.headers)
            # print(f'data:>>>{data}')
            self.save_image(file_name,data)
if __name__ == '__main__':
    url = 'https://pic.netbian.com/4kmeinv'
    s = S_wm()
    if os.path.exists("./wm") is False:
        os.mkdir('./wm')
    s.run(url)